<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once "class/Load.class.php";
require_once "class/Config.class.php";
require_once "class/Inputs.class.php";
require_once "class/Tooltip.class.php";
require_once "class/StatusRoster.class.php";
require_once "class/SessionRoster.class.php";
require_once "class/Localization.class.php";
require_once "class/ObjectContainer.class.php";
require_once "class/WowTable.class.php";
require_once "class/SubscriptionManager.class.php";
require_once "class/SubscriptionDisplayer.class.php";

$load = Load::GetInstance();
$config = Config::GetInstance();
$inputs = Inputs::GetInstance();
$tooltip = Tooltip::GetInstance();
$localization = Localization::GetInstance();
$status_roster = StatusRoster::GetInstance();
$session_roster = SessionRoster::GetInstance();
$object_container = ObjectContainer::GetInstance();
$subscription_manager = SubscriptionManager::GetInstance();
$subscription_displayer = SubscriptionDisplayer::GetInstance();

if ( $session_roster->IsAdminConnected() == false )
{
	$session_roster->Redirect("admin_connection.php");
}

if ( $inputs->HasPostData('reset_tooltips') )
{
	if ( $object_container->ResetTooltips() == false )
	{
		$status_roster->AddError( $localization->Get('tooltips could not be reseted') );
	}
	else
	{
		$status_roster->AddSuccess( $localization->Get('tooltips reseted. they will be recomputed at the next (...)') );
	}
	
	$session_roster->SaveStatus($status_roster);
	$session_roster->Redirect("admin.php");
}

if ( $inputs->HasPostData('a_chg_nickname') )
{
	
	if ( $inputs->Post('new_nickname') == null )
	{
		$status_roster->AddError( $localization->Get('you must enter a nickname') );
	}
	elseif ( $subscription_manager->HasNickname($inputs->Post('new_nickname')) == SUBSCRIPTION_MANAGER_NICKNAME_EXISTS )
	{
		$status_roster->AddError( $localization->Get('this nickname already exists') );
	}
	elseif ( $subscription_manager->HasName($inputs->Post('name')) == SUBSCRIPTION_MANAGER_DOES_NOT_EXIST )
	{
		$status_roster->AddError( $localization->Get('this account does not exist') );
	}
	elseif ( $subscription_manager->UpdateNickname( $subscription_manager->GetAccountIdByName($inputs->Post('name')), $inputs->Post('new_nickname')) == false )
	{
		$status_roster->AddError( $localization->Get('could not update nickname') );
	}
	else
	{
		$status_roster->AddSuccess( $localization->Get('nickname updated successfully for account %s', $subscription_manager->GetSubscription($subscription_manager->GetAccountIdByName($inputs->Post('name')))->GetName() ) );
	}
	
	$session_roster->SaveStatus($status_roster);
	$session_roster->Redirect("admin.php");
}

if ( $inputs->HasPostData('a_chg_pwd') )
{
	if ( $subscription_manager->CheckPassword($inputs->Post('new_pass')) == false )
	{
		$status_roster->AddError( $localization->Get('the password is too short') );
	}
	elseif ( $subscription_manager->HasName($inputs->Post('name')) == SUBSCRIPTION_MANAGER_DOES_NOT_EXIST )
	{
		$status_roster->AddError( $localization->Get('this account does not exist') );
	}
	elseif ( $subscription_manager->SetPassword( $subscription_manager->GetAccountIdByName($inputs->Post('name')), $inputs->Post('new_pass')) == false )
	{
		$status_roster->AddError( $localization->Get('could not update password') );
	}
	else
	{
		$status_roster->AddSuccess( $localization->Get('password updated successfully for account %s', $subscription_manager->GetSubscription($subscription_manager->GetAccountIdByName($inputs->Post('name')))->GetName() ) );
	}
	
	$session_roster->SaveStatus($status_roster);
	$session_roster->Redirect("admin.php");
}

if ( $inputs->HasPostData('a_del') )
{
	if ( $subscription_manager->HasName($inputs->Post('name')) == SUBSCRIPTION_MANAGER_DOES_NOT_EXIST )
	{
		$status_roster->AddError( $localization->Get('this account does not exist') );
	}
	elseif ( $subscription_manager->GetAccountIdByName($inputs->Post('name')) == $session_roster->GetAccountId() )
	{
		$status_roster->AddError( $localization->Get('you cannot delete your own account') );
	}
	elseif ( $subscription_manager->Delete( $subscription_manager->GetAccountIdByName($inputs->Post('name')) ) == false )
	{
		$status_roster->AddError( $localization->Get('could not delete account') );
	}
	else
	{
		$status_roster->AddSuccess( $localization->Get('account deleted successfully: %s', $subscription_manager->GetSubscription($subscription_manager->GetAccountIdByName($inputs->Post('name')))->GetName() ) );
	}
	
	$session_roster->SaveStatus($status_roster);
	$session_roster->Redirect("admin.php");
}

$wow_table = new WowTable( $localization->Get('accounts') );
$wow_table->AddSubSection();
$wow_table->AddColumn( $localization->Get('account') );
$wow_table->AddColumn( $localization->Get('change nickname'), "center", 250 );
$wow_table->AddColumn( $localization->Get('change password'), "center", 270 );
$wow_table->AddColumn( $localization->Get('delete account'), "center", 125 );

foreach ($subscription_manager->GetAllAccounts() as $subscription)
{
	$wow_table->AddLine(0, array(
		$subscription_displayer->Name($subscription),
		$subscription_displayer->__ChangeNickname($subscription),
		$subscription_displayer->__ChangePassword($subscription),
		$subscription_displayer->__Delete($subscription)
	));
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
	<head>
		<title>LZRoster<?php echo $config->HasRosterTitle() ?  " - ".$config->GetRosterTitle() : null; ?></title>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/_.css" />
		<link rel="stylesheet" media="screen" type="text/css" href="styles/wowtable.css" />
		<script type="text/javascript" src="js/overlib.js"><!-- overLIB (c) Erik Bosrup --></script>
	</head>
	<body>
		
		<?php require_once "inc/head.php"; ?>
		<?php require_once "inc/hmenu.php"; ?>
		
		<h2><?php echo $localization->Get('admin'); ?></h2>
		
		<?php
		if ($status_roster->HasMessage() or $session_roster->HasStatus())
		{
		?>
			
			<p>
				<?php echo $localization->Get('this page produced some messages. please refer to the following lines'); ?>
			</p>
			
			<?php echo $session_roster->HasStatus() ? $session_roster->GetStatus()->GetStack_InList() : $status_roster->GetStack_InList(); ?>
			
			<p>
				<?php echo $localization->Get('you can refresh the page %s and try again', "admin.php"); ?>
			</p>
			
		<?php
		}
		else
		{
		?>
		
		<h3><?php echo $localization->Get('reset tooltips'); ?></h3>
		
		<p>
			<?php echo $localization->Get('use this tool to reset the tooltips of all the items and spells of the roster. this can be used (...)'); ?>
		</p>
		
		<form action="admin.php" method="post">
		<p>
			<?php echo $localization->Get('you can reset the tooltips by clicking on this button'); ?>
			<input type="submit" name="reset_tooltips" value="<?php echo $localization->Get('reset tooltips'); ?>" onclick="return confirm('<?php echo $tooltip->Flatten($localization->Get('are you sure you want to reset the tooltips')); ?>');" style="vertical-align: middle;" />
		</p>
		</form>
		
		<h3><?php echo $localization->Get('update/delete account'); ?></h3>
		
		<?php $wow_table->GetAndPrintTable(); ?>
		
		<?php
		}
		?>
		
		<?php require_once "inc/foot.php"; ?>
		
	</body>
</html>